package cn.com.hhrcw.statistics.impl;

import cn.com.hhrcw.entity.OpDeliveryHistory;
import cn.com.hhrcw.entity.OpEntryHistory;
import cn.com.hhrcw.entity.OpInterviewHistory;
import cn.com.hhrcw.entity.OpJob;
import cn.com.hhrcw.mapper.OpDeliveryHistoryMapper;
import cn.com.hhrcw.mapper.OpEntryHistoryMapper;
import cn.com.hhrcw.mapper.OpInterviewHistoryMapper;
import cn.com.hhrcw.mapper.OpJobMapper;
import cn.com.hhrcw.statistics.IStatisticsService;
import cn.com.hhrcw.statistics.entity.Statistics;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lixingxing
 */
@Service
@Slf4j
public class StatisticsServiceImpl implements IStatisticsService {
    @Autowired
    private OpJobMapper jobMapper;

    @Autowired
    private OpDeliveryHistoryMapper deliveryHistoryMapper;

    @Autowired
    private OpInterviewHistoryMapper interviewHistoryMapper;

    @Autowired
    private OpEntryHistoryMapper entryHistoryMapper;

    @Override
    public Statistics queryByJob(String job) {
        Statistics statistics = new Statistics();
        OpJob opJob = jobMapper.selectById(job);
        //招聘人数
        statistics.setRecruitment(opJob.getNeedCount());
        LambdaQueryWrapper<OpDeliveryHistory> deliveryHistoryLambdaQueryWrapper = Wrappers.<OpDeliveryHistory>lambdaQuery().eq(OpDeliveryHistory::getJobId, job).eq(OpDeliveryHistory::getDelFlag, false);
        List<OpDeliveryHistory> opDeliveryHistories = deliveryHistoryMapper.selectList(deliveryHistoryLambdaQueryWrapper);
        statistics.setTotal(opDeliveryHistories.size());
        statistics.setFilterFirst((int) opDeliveryHistories.stream().filter(e -> e.getState() >= 3 && e.getScreenFlag()).count());
        statistics.setFilterBySystem((int) opDeliveryHistories.stream().filter(e -> !e.getScreenFlag()).count());
        //面试
        LambdaQueryWrapper<OpInterviewHistory> interviewHistoryLambdaQueryWrapper = Wrappers.<OpInterviewHistory>lambdaQuery().eq(OpInterviewHistory::getJobId, job).eq(OpInterviewHistory::getDelFlag, false);
        List<OpInterviewHistory> opInterviewHistories = interviewHistoryMapper.selectList(interviewHistoryLambdaQueryWrapper);
        statistics.setInterview(opInterviewHistories.size());
        statistics.setAcceptInterview((int) opInterviewHistories.stream().filter(e -> e.getOutLineState() == 1).count());
        statistics.setAlreadyInterview((int) opInterviewHistories.stream().filter(e -> e.getViewResult() != null).count());
        statistics.setPassInterview((int) opInterviewHistories.stream().filter(e -> e.getViewResult() != null && e.getViewResult() == 0).count());
        //入职邀请
        LambdaQueryWrapper<OpEntryHistory> entryHistoryLambdaQueryWrapper = Wrappers.<OpEntryHistory>lambdaQuery().eq(OpEntryHistory::getJobId, job).eq(OpEntryHistory::getDelFlag, false);
        List<OpEntryHistory> opEntryHistories = entryHistoryMapper.selectList(entryHistoryLambdaQueryWrapper);
        statistics.setOffer(opEntryHistories.size());
        statistics.setAcceptOffer((int) opEntryHistories.stream().filter(e -> e.getAcceptFlag() == 1).count());
        statistics.setRejectOffer((int) opEntryHistories.stream().filter(e -> e.getAcceptFlag() == 2).count());
        statistics.setAccession((int) opEntryHistories.stream().filter(e -> e.getEntryFlag() != null && e.getEntryFlag()).count());
        return statistics;
    }

    @Override
    public Statistics queryByCompany(String companyId) {
        Statistics statistics = new Statistics();
        LambdaQueryWrapper<OpJob> wrapper = Wrappers.<OpJob>lambdaQuery().eq(OpJob::getCompanyId, companyId).eq(OpJob::getRecruitmentState, 0).eq(OpJob::getDelFlag, false);
        List<OpJob> opJobs = jobMapper.selectList(wrapper);
        //招聘人数
        statistics.setRecruitment(opJobs.stream().mapToInt(OpJob::getNeedCount).sum());
        Object[] array = opJobs.stream().map(OpJob::getId).toArray();
        if (array == null || array.length == 0) {
            array = new java.lang.String[]{""};
        }
        LambdaQueryWrapper<OpDeliveryHistory> deliveryHistoryLambdaQueryWrapper = Wrappers.<OpDeliveryHistory>lambdaQuery().in(OpDeliveryHistory::getJobId, array).eq(OpDeliveryHistory::getDelFlag, false);
        List<OpDeliveryHistory> opDeliveryHistories = deliveryHistoryMapper.selectList(deliveryHistoryLambdaQueryWrapper);
        statistics.setTotal(opDeliveryHistories.size());
        statistics.setFilterFirst((int) opDeliveryHistories.stream().filter(e -> e.getState() >= 3 && e.getScreenFlag()).count());
        statistics.setFilterBySystem((int) opDeliveryHistories.stream().filter(e -> !e.getScreenFlag()).count());
        //面试
        LambdaQueryWrapper<OpInterviewHistory> interviewHistoryLambdaQueryWrapper = Wrappers.<OpInterviewHistory>lambdaQuery().in(OpInterviewHistory::getJobId, array).eq(OpInterviewHistory::getDelFlag, false);
        List<OpInterviewHistory> opInterviewHistories = interviewHistoryMapper.selectList(interviewHistoryLambdaQueryWrapper);
        statistics.setInterview(opInterviewHistories.size());
        statistics.setAcceptInterview((int) opInterviewHistories.stream().filter(e -> e.getOutLineState() == 1).count());
        statistics.setAlreadyInterview((int) opInterviewHistories.stream().filter(e -> e.getViewResult() != null).count());
        statistics.setPassInterview((int) opInterviewHistories.stream().filter(e -> e.getViewResult() != null && e.getViewResult() == 0).count());
        //入职邀请
        LambdaQueryWrapper<OpEntryHistory> entryHistoryLambdaQueryWrapper = Wrappers.<OpEntryHistory>lambdaQuery().in(OpEntryHistory::getJobId, array).eq(OpEntryHistory::getDelFlag, false);
        List<OpEntryHistory> opEntryHistories = entryHistoryMapper.selectList(entryHistoryLambdaQueryWrapper);
        statistics.setOffer(opEntryHistories.size());
        statistics.setAcceptOffer((int) opEntryHistories.stream().filter(e -> e.getAcceptFlag() == 1).count());
        statistics.setRejectOffer((int) opEntryHistories.stream().filter(e -> e.getAcceptFlag() == 2).count());
        statistics.setAccession((int) opEntryHistories.stream().filter(e -> e.getEntryFlag() != null && e.getEntryFlag()).count());
        return statistics;
    }
}
